Customizing Output in Debugger Display

Without

public class PersonWithoutDebuggerDisplay
    {
        public int AgeInYears { get; set; }
        public string Name { get; set; }
    }

Without attribute

Overriding .ToString()

public class PersonWithOverridenToString
    {
        public int AgeInYears { get; set; }
        public string Name { get; set; }

        public override string ToString()
        {
            return "Overriden ToString()";
        }
    }

Overriding ToString

Using DebuggerDisplay

[DebuggerDisplay("This person is called {Name} and is {AgeInYears} years old")]
    public class PersonWithDebuggerDisplay
    {
        [DebuggerDisplay("{AgeInYears} years old")]
        public int AgeInYears { get; set; }

        [DebuggerDisplay("Hello {Name}")]
        public string Name { get; set; }
    }

Refer to member by enclosing them in curly braces {AgeInYears}

With DebuggerDisplay

Note: DebuggerDisplayAttribute on property is not working due to a bug in Visual Studo

See Also

DebuggerDisplay attribute best practices

OzCode

Full Code

using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DebuggerDisplayAttributeExample
{
    // Add breakpoint and debug the tests
    [TestClass]
    public class DebuggerDisplayAttributeExample
    {
        [TestMethod]
        public void WithoutDebuggerDisplay()
        {
            var p = new PersonWithoutDebuggerDisplay()
            {
                AgeInYears = 12,
                Name = "Test"
            };
        }

        [TestMethod]
        public void OverridingToString()
        {
            var p = new PersonWithOverridenToString()
            {
                AgeInYears = 12,
                Name = "Test"
            };
        }

        [TestMethod]
        public void WithDebuggerDisplay()
        {
            var p = new PersonWithDebuggerDisplay()
            {
                AgeInYears = 12,
                Name = "Test"
            };
        }
    }

    public class PersonWithoutDebuggerDisplay
    {
        public int AgeInYears { get; set; }
        public string Name { get; set; }
    }

    public class PersonWithOverridenToString
    {
        public int AgeInYears { get; set; }
        public string Name { get; set; }

        public override string ToString()
        {
            return "Overriden ToString()";
        }
    }

    [DebuggerDisplay("This person is called {Name} and is {AgeInYears} years old")]
    public class PersonWithDebuggerDisplay
    {
        [DebuggerDisplay("{AgeInYears} years old")]
        public int AgeInYears { get; set; }

        [DebuggerDisplay("Hello {Name}")]
        public string Name { get; set; }
    }

}

results matching ""

    No results matching ""